package com.dimples.dd.xxl.job.plus.discovery.nacos;

import com.alibaba.boot.nacos.discovery.properties.NacosDiscoveryProperties;
import com.alibaba.boot.nacos.discovery.properties.Register;
import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.utils.NetUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Component;

/**
 * nacos namingService-默认
 *
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2024/10/18
 */
@Component
@ConditionalOnMissingBean(NamingServiceHolder.class)
@RequiredArgsConstructor
public final class DefaultNamingServiceHolder implements NamingServiceHolder {

    @NacosInjected
    private NamingService namingService;

    private final NacosDiscoveryProperties discoveryProperties;

    @Value("${spring.application.name:}")
    private String applicationName;

    @Value("${server.port}")
    private Integer port;

    @Override
    public NamingService get() {
        return namingService;
    }

    @Override
    public String getExecutorAddress() {

        Register register = discoveryProperties.getRegister();

        if (StringUtils.isEmpty(register.getIp())) {
            register.setIp(NetUtils.localIP());
        }

        if (register.getPort() == 0) {
            register.setPort(port);
        }

        return register.getIp() + ":" + register.getPort();
    }

    @Override
    public String getServiceName() {
        String serviceName = discoveryProperties.getRegister().getServiceName();

        if (StringUtils.isEmpty(serviceName)) {
            serviceName = applicationName;
        }
        return serviceName;
    }
}
